/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* **********************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using OfficeOpenXMLInterop;

namespace Theme_Builder
{
    public partial class EditEffect : UserControl
    {
        public EditEffect()
        {
            InitializeComponent();
        }

        public override string Text
        {
            get
            {
                return this.labelName.Text;
            }
            set
            {
                this.labelName.Text = value;
            }
        }

        public OfficeOpenXMLInterop.CT_EffectList EffectList
        {
            get
            {
                CT_EffectList effectList = new CT_EffectList();

                // Grab all the effects in the list and add them to our effect list
                foreach (object obj in this.listBoxEffects.Items)
                {
                    // We're expecting at max, 1, of any effect

                    if (obj.GetType() == typeof(CT_OuterShadowEffect))
                    {
                        effectList.outerShdw = (CT_OuterShadowEffect)obj;
                    }
                    else if (obj.GetType() == typeof(CT_InnerShadowEffect))
                    {
                        effectList.innerShdw = (CT_InnerShadowEffect)obj;
                    }
                    else if(obj.GetType() == typeof(CT_GlowEffect))
                    {
                        effectList.glow = (CT_GlowEffect)obj;
                    }
                    else if(obj.GetType() == typeof(CT_ReflectionEffect))
                    {
                        effectList.reflection = (CT_ReflectionEffect)obj;
                    }
                    else if(obj.GetType() == typeof(CT_SoftEdgesEffect))
                    {
                        effectList.softEdge = (CT_SoftEdgesEffect)obj;
                    }
                }

                return effectList;
            }
            set
            {
                this.listBoxEffects.Items.Clear();

                if (value == null)
                    return;

                if (value.outerShdw != null)
                {
                    this.listBoxEffects.Items.Add(value.outerShdw);
                }
                if (value.innerShdw != null)
                {
                    this.listBoxEffects.Items.Add(value.innerShdw);
                }
                if (value.glow != null)
                {
                    this.listBoxEffects.Items.Add(value.glow);
                }
                if (value.reflection != null)
                {
                    this.listBoxEffects.Items.Add(value.reflection);
                }
                if (value.softEdge != null)
                {
                    this.listBoxEffects.Items.Add(value.softEdge);
                }
            }
        }

        private void comboBoxEffectsAvailable_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.buttonAddEffect.Enabled = false;

            if (this.comboBoxEffectsAvailable.SelectedIndex >= 0)
            {
                this.buttonAddEffect.Enabled = true;

                Type selectedType = Type.Missing.GetType();

                switch (this.comboBoxEffectsAvailable.SelectedItem.ToString())
                {
                    case "Outer Shadow":
                        selectedType = typeof(CT_OuterShadowEffect);
                        break;
                    case "Inner Shadow":
                        selectedType = typeof(CT_InnerShadowEffect);
                        break;
                    case "Glow":
                        selectedType = typeof(CT_GlowEffect);
                        break;
                    case "Reflection":
                        selectedType = typeof(CT_ReflectionEffect);
                        break;
                    case "Soft Edges":
                        selectedType = typeof(CT_SoftEdgesEffect);
                        break;
                }

                // See if we already have one of these types in our list of effects
                foreach (object obj in this.listBoxEffects.Items)
                {
                    if(obj.GetType() == selectedType)
                    {
                        this.buttonAddEffect.Enabled = false;
                    }
                }
            }
        }

        private void buttonRemoveEffect_Click(object sender, EventArgs e)
        {
            if (this.listBoxEffects.SelectedItem != null)
            {
                this.listBoxEffects.Items.Remove(this.listBoxEffects.SelectedItem);
                this.buttonRemoveEffect.Enabled = false;
                this.buttonEditEffect.Enabled = false;
                this.comboBoxEffectsAvailable.SelectedIndex = -1;
            }
        }

        private void listBoxEffects_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listBoxEffects.SelectedItem != null)
            {
                this.buttonEditEffect.Enabled = true;
                this.buttonRemoveEffect.Enabled = true;
            }
        }

        private void buttonAddEffect_Click(object sender, EventArgs e)
        {
            if (this.comboBoxEffectsAvailable.SelectedItem != null)
            {

                // Create some default effects and add them to our list of effects
                switch (this.comboBoxEffectsAvailable.SelectedItem.ToString())
                {
                    case "Outer Shadow":

                        CT_OuterShadowEffect outerShadow = new CT_OuterShadowEffect();

                        outerShadow.srgbClr = new CT_SRgbColor();
                        outerShadow.srgbClr.val = Converters.ColorToByteRBG(Color.Black);
                        outerShadow.srgbClr.alpha = new CT_PositiveFixedPercentage();
                        outerShadow.srgbClr.alpha.val = 75000;

                        this.listBoxEffects.Items.Add(outerShadow);

                        break;
                    case "Inner Shadow":

                        CT_InnerShadowEffect innerShadow = new CT_InnerShadowEffect();
                        innerShadow.srgbClr = new CT_SRgbColor();
                        innerShadow.srgbClr.val = Converters.ColorToByteRBG(Color.Black);
                        innerShadow.srgbClr.alpha = new CT_PositiveFixedPercentage();
                        innerShadow.srgbClr.alpha.val = 75000;

                        this.listBoxEffects.Items.Add(innerShadow);

                        break;
                    case "Glow":

                        CT_GlowEffect glow = new CT_GlowEffect();
                        glow.schemeClr = new CT_SchemeColor();
                        glow.schemeClr.val = ST_SchemeColorVal.phClr;

                        this.listBoxEffects.Items.Add(glow);

                        break;
                    case "Reflection":

                        CT_ReflectionEffect reflection = new CT_ReflectionEffect();

                        this.listBoxEffects.Items.Add(reflection);

                        break;
                    case "Soft Edges":

                        CT_SoftEdgesEffect softEdge = new CT_SoftEdgesEffect();

                        this.listBoxEffects.Items.Add(softEdge);

                        break;
                }

                this.comboBoxEffectsAvailable.SelectedIndex = -1;
            }
        }

        private void buttonEditEffect_Click(object sender, EventArgs e)
        {
            object selectedItem = this.listBoxEffects.SelectedItem;

            if (selectedItem == null)
                return;

            // Find out what type of effect we have selected and handle it accordingly
            if (selectedItem.GetType() == typeof(CT_OuterShadowEffect))
            {
                // Handle an outer shadow edit
                EditOuterShadow eos = new EditOuterShadow();
                eos.OuterShadow = (CT_OuterShadowEffect)selectedItem;

                if (eos.ShowDialog(this) == DialogResult.OK)
                {
                    int idx = this.listBoxEffects.SelectedIndex;
                    this.listBoxEffects.Items.Insert(idx, eos.OuterShadow);
                    this.listBoxEffects.Items.Remove(selectedItem);
                    this.listBoxEffects.SelectedIndex = idx;
                }
            }
            else if (selectedItem.GetType() == typeof(CT_InnerShadowEffect))
            {
                EditInnerShadow eis = new EditInnerShadow();
                eis.InnerShadow = (CT_InnerShadowEffect)selectedItem;

                if (eis.ShowDialog(this) == DialogResult.OK)
                {
                    int idx = this.listBoxEffects.SelectedIndex;
                    this.listBoxEffects.Items.Insert(idx, eis.InnerShadow);
                    this.listBoxEffects.Items.Remove(selectedItem);
                    this.listBoxEffects.SelectedIndex = idx;
                }
            }
            else if (selectedItem.GetType() == typeof(CT_GlowEffect))
            {
                EditGlow eg = new EditGlow();
                eg.Glow = (CT_GlowEffect)selectedItem;

                if (eg.ShowDialog(this) == DialogResult.OK)
                {
                    int idx = this.listBoxEffects.SelectedIndex;
                    this.listBoxEffects.Items.Insert(idx, eg.Glow);
                    this.listBoxEffects.Items.Remove(selectedItem);
                    this.listBoxEffects.SelectedIndex = idx;
                }
            }
            else if (selectedItem.GetType() == typeof(CT_SoftEdgesEffect))
            {
                EditSoftEdge ese = new EditSoftEdge();
                ese.SoftEdge = (CT_SoftEdgesEffect)selectedItem;

                if (ese.ShowDialog(this) == DialogResult.OK)
                {
                    int idx = this.listBoxEffects.SelectedIndex;
                    this.listBoxEffects.Items.Insert(idx, ese.SoftEdge);
                    this.listBoxEffects.Items.Remove(selectedItem);
                    this.listBoxEffects.SelectedIndex = idx;
                }
            }
            else if (selectedItem.GetType() == typeof(CT_ReflectionEffect))
            {
                EditReflection er = new EditReflection();
                er.Reflection = (CT_ReflectionEffect)selectedItem;

                if (er.ShowDialog(this) == DialogResult.OK)
                {
                    int idx = this.listBoxEffects.SelectedIndex;
                    this.listBoxEffects.Items.Insert(idx, er.Reflection);
                    this.listBoxEffects.Items.Remove(selectedItem);
                    this.listBoxEffects.SelectedIndex = idx;
                }
            }
        }

        private void listBoxEffects_DoubleClick(object sender, EventArgs e)
        {
            this.buttonEditEffect_Click(sender, e);
        }
    }
}
